package com.example.shiwu.adapter;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.example.shiwu.R;
import com.example.shiwu.SendReceiveImgDetailsActivity;
import com.example.shiwu.SendReceiveListActivity;
import com.example.shiwu.db.model.Sends;
import com.example.shiwu.db.vo.SendVo;
import com.example.shiwu.service.SendsService;
import com.example.shiwu.util.FileDownUtil;

import java.util.List;

public class SendReceiveAdapter extends BaseAdapter implements View.OnClickListener{

    private SendReceiveListActivity mContext;
    private int resourceId;
    public boolean flage = false;
    private List<SendVo> listItems;
    private LayoutInflater mInflater;
    private String[] myModaData,freqData,freqFilterData, repeatData, valtageData;
    private Integer checkBoxIDList;            //存储checkBox的值
    private MyClickListener mListener,rListener;
    //自定义接口，用于回调按钮点击事件到Activity
    public interface MyClickListener{
        public void clickListener(View v);
        boolean clickRemoveListener(int position);
    }



    public SendReceiveAdapter(SendReceiveListActivity mContext, List<SendVo> listItems, int resource, String[] modeData,
                              String[] freqData,String[] repeatData,String[] valtageData,MyClickListener listener) {
        this.resourceId = resource;
        this.mContext = mContext;
        this.listItems = listItems;
        this.myModaData = modeData;
        this.freqData = freqData;

        this.repeatData = repeatData;
        this.valtageData = valtageData;
        this.mListener = listener;
        this.rListener = listener;
    }
    public void cleckAll() {
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        final ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(parent.getContext()).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder(view, position);
            viewHolder.device_id.setText(listItems.get(position).getDeviceId());
            viewHolder.send_items_name.setText(listItems.get(position).getName());
            //System.out.println(valtageData[Integer.parseInt(listItems.get(position).getVoltages())]);
            viewHolder.send_items_voltage.setText(valtageData[Integer.parseInt(listItems.get(position).getVoltages())]+"V");
            viewHolder.send_device_freq.setText(freqData[Integer.parseInt(listItems.get(position).getSendFrequency())]+"Hz");
            viewHolder.send_device_repeat_num.setText(repeatData[Integer.parseInt(listItems.get(position).getRepeatNum())]);
            //viewHolder.send_items_electricity.setText(listItems.get(position).getElectricity());
//            viewHolder.send_item_location.setText(listItems.get(position).getSendLocal());
//            viewHolder.receive_item_location.setText(listItems.get(position).getReceiveLocal());
            //viewHolder.send_device_type.setText(listItems.get(position).getDeviceType());
            viewHolder.send_device_type.setText(myModaData[listItems.get(position).getDeviceType()]);
            viewHolder.send_operator.setText(String.valueOf(listItems.get(position).getOperator()));
            //System.out.println("listItems.get(position).getCreateTime()000 ============================"+listItems.get(position).getCreateTime());
            viewHolder.send_items_createTime.setText(listItems.get(position).getCreateTime());
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (SendReceiveAdapter.ViewHolder) view.getTag();
            viewHolder.device_id.setText(listItems.get(position).getDeviceId());
            viewHolder.send_items_name.setText(listItems.get(position).getName());
            viewHolder.send_items_voltage.setText(valtageData[Integer.parseInt(listItems.get(position).getVoltages())]+"V");
            viewHolder.send_device_freq.setText(freqData[Integer.parseInt(listItems.get(position).getSendFrequency())]+"Hz");
            viewHolder.send_device_repeat_num.setText(repeatData[Integer.parseInt(listItems.get(position).getRepeatNum())]);
            //viewHolder.send_items_electricity.setText(listItems.get(position).getElectricity());
//            viewHolder.send_item_location.setText(listItems.get(position).getSendLocal());
//            viewHolder.receive_item_location.setText(listItems.get(position).getReceiveLocal());
            //viewHolder.send_device_type.setText(listItems.get(position).getDeviceType());
            viewHolder.send_device_type.setText(myModaData[listItems.get(position).getDeviceType()]);
            viewHolder.send_operator.setText(String.valueOf(listItems.get(position).getOperator()));
            //System.out.println("listItems.get(position).getCreateTime()111 ============================"+listItems.get(position).getCreateTime());
            viewHolder.send_items_createTime.setText(listItems.get(position).getCreateTime());
        }


        viewHolder.detailsSub.setOnClickListener(new View.OnClickListener() {//详情
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, SendReceiveImgDetailsActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Integer sId = Integer.parseInt(listItems.get(position).getId());
                //Integer itemId = listItems.get(position).getItemId();
                intent.putExtra("id",String.valueOf(sId));
                mContext.startActivity(intent);
            }
        });

        viewHolder.delSb.setOnClickListener(new View.OnClickListener() {//删除
            @Override
            public void onClick(View v) {
                System.out.println("id =================================== " + v.getId());
                //点击删除按钮之后，给出dialog提示
                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                builder.setTitle("确认删除?");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        System.out.println("id =================================== " + position);
                        System.out.println("——————————————删除——————————————————");
                        Integer sId = Integer.parseInt(listItems.get(position).getId());
                        Integer itemId = listItems.get(position).getItemId();

                        Sends sen1 = new Sends(itemId);
                        sen1.setStatus(1);
                        String uid = listItems.get(position).getUid();
                        SendsService sendService = new SendsService();
                        Sends sen2 = new Sends();
                        sen2.setId(sId);
                        Boolean bool = sendService.updade(sen1, sen2);//先删表数据在删除文件
                        //Boolean bool = sendService.delete(new Sends(itemId));
//                        if (bool) {
//                            //boolean bol = rListener.clickRemoveListener(position);
//
//                        }

                        if (bool) {
                            mContext.updataData();
                            String fileName = String.valueOf(itemId) + "-" + uid + ".cvs";
                            Boolean bool1 = FileDownUtil.deleteSingleFile(FileDownUtil.DOWN_NAME + fileName);
                            if (bool1) {
                                SendVo deleteNumber = listItems.get(listItems.size() - 1);
                                Boolean bool2 = listItems.remove(deleteNumber);
                                if(bool2){
//                                    listItems.remove(position);
//                                    notifyDataSetChanged();
                                    rListener.clickRemoveListener(position);
                                    Toast.makeText(v.getContext(), "删除成功！", Toast.LENGTH_SHORT).show();
                                }
                            }else {
                                SendVo deleteNumber = listItems.get(listItems.size() - 1);
                                Boolean bool2 = listItems.remove(deleteNumber);
                                if(bool2){
                                    rListener.clickRemoveListener(position);
                                    Toast.makeText(v.getContext(), "删除成功！", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }

                    }
                });
                builder.show();
            }
        });
        //viewHolder.checkboxOperateData = (CheckBox) convertView.findViewById(R.id.device_items_checkbox);
        // 根据isSelected来设置checkbox的显示状况
//        if (flage) {
//            holder.checkboxOperateData.setVisibility(View.VISIBLE);
//        } else {
//            holder.checkboxOperateData.setVisibility(View.GONE);
//        }
        //获取复选框选中状态改变事件进行增删改
        viewHolder.send_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                /*
                 * b=选中状态
                 * if b = true 将值添加至checkBoxIDList
                 * if b = false 将值从checkBoxIDList移除
                 * */
                Integer itemId = Integer.parseInt(listItems.get(position).getId());
                if (b) {

                    checkBoxIDList = itemId;

                } else {

                    checkBoxIDList = -itemId;

                }
                System.out.println(checkBoxIDList);
                compoundButton.setId(checkBoxIDList);
                mListener.clickListener(compoundButton);
            }
        });

        return view;
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {
        return listItems.get(position);
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    //响应按钮点击事件,调用子定义接口，并传入View
    @Override
    public void onClick(View v) {
        mListener.clickListener(v);
    }
    class ViewHolder {

//        public TextView detailsSub,delSb, device_id,send_items_name, send_items_voltage, send_items_electricity,
//                send_item_location,receive_item_location,send_device_type,send_operator,send_items_createTime;
        public TextView detailsSub,delSb, device_id,send_items_name, send_items_voltage, send_device_freq,
        send_device_repeat_num,send_device_type,send_operator,send_items_createTime;
        public CheckBox send_checkbox;

        public ViewHolder(View view, int position) {
            device_id = view.findViewById(R.id.device_id_val);
            send_items_name = view.findViewById(R.id.send_items_name_val);
            send_items_voltage = view.findViewById(R.id.send_items_voltage_val);
            send_device_freq = view.findViewById(R.id.send_device_freq_val);
            send_device_repeat_num = view.findViewById(R.id.send_device_repeat_num_val);
            //send_items_electricity = view.findViewById(R.id.send_items_electricity_val);
//            send_item_location = view.findViewById(R.id.send_item_location_val);
//            receive_item_location = view.findViewById(R.id.receive_item_location_val);
            send_device_type = view.findViewById(R.id.send_device_type_val);
            send_operator = view.findViewById(R.id.send_operator_val);
            send_items_createTime = view.findViewById(R.id.send_items_createTime_val);
            detailsSub = view.findViewById(R.id.send_items_details_sb);
            delSb = view.findViewById(R.id.send_items_del_sb);
            send_checkbox = view.findViewById(R.id.send_list_items_checkbox);
        }

    }

}
